﻿using System.Collections.Generic;

public class NetHandler_DebugInfo : NetMessageHandler
{
    public const string Key = "DebugInfo";

    public static NetHandler_DebugInfo NewHandler()
    {
        return new NetHandler_DebugInfo();
    }

    List<BattleServerInfo> battleDebugInfos;

    protected override ISerialize NewData() => new StringResponseData();

    public override void Execute(Remote client, ISerialize _data)
    {
        GameDebug.Log($"Master  请求DebugInfo  ");

        battleDebugInfos = new List<BattleServerInfo>();

        int count = 0;
        int battleServerCount = Game.instance.battleConnects.Length;

        foreach (var battleServer in Game.instance.battleConnects)
        {
            NetSend_GetBattleDebugInfo netSend_GetBattleDebugInfo = new NetSend_GetBattleDebugInfo();
            netSend_GetBattleDebugInfo.onResponse = (res) =>
            {
                StringResponseData stringResponseData = res as StringResponseData;

                //GameDebug.Log($"收到 {battleServer.ipEndPoint.Address} BattleDebugInfo:{stringResponseData.value}");

                count++;
                if (stringResponseData != null && !string.IsNullOrEmpty(stringResponseData.value))
                {
                    battleDebugInfos.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<BattleServerInfo>(stringResponseData.value));
                }
                if(count >= battleServerCount)
                {
                    var battles = this.battleDebugInfos;
                    SendResponse(client, Game.instance.GetDebugInfo(), battles);
                    this.battleDebugInfos = null;
                }
            };
            netSend_GetBattleDebugInfo.onTimeOut = (req) =>
            {
                GameDebug.Log($"{battleServer.ipEndPoint.Address} 请求DebugInfo超时");

                count++; 
                if (count >= battleServerCount)
                {
                    var battles = this.battleDebugInfos;
                    SendResponse(client, Game.instance.GetDebugInfo(), battles);
                    battleDebugInfos = null;
                }
            };

            netSend_GetBattleDebugInfo.SendToRemote(battleServer);
        }
    }

    void SendResponse(Remote client, LogicServerInfo logicInfo,List<BattleServerInfo> battleInfos)
    {
        GameDebugInfo gameDebugInfo = new GameDebugInfo();

        gameDebugInfo.logicServerInfo = logicInfo;
        gameDebugInfo.battleServerInfo = new Dictionary<string, BattleServerInfo>();
        foreach (var battleInfo in battleInfos)
        {
            gameDebugInfo.battleServerInfo[battleInfo.serverIp] = battleInfo;
        }

        StringResponseData response = new StringResponseData();
        response.code = 0;
        response.value = gameDebugInfo.ToString();
        byte[] senddata = response.Write();

        GameDebug.Log($"DebugInfo SendResponse：{ response.value }");

        this.SendResponse(client, Key, this.m_MessageReceived.seq, senddata);
    }

}